﻿using System;
using System.Text;

namespace EasyMvc.Utility
{
    public static class Converter
    {
        public static string ToCnDataString(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd");
        }

        public static string ToCnDataString(this DateTime? dateTime)
        {
            return dateTime == null ? string.Empty : dateTime.Value.ToCnDataString();
        }

        public static string ToPrice(this decimal price, string format = "0.00")
        {
            return price.ToString(format);
        }

        public static string ToShortPriceRange(this decimal fromPrice, decimal toPrice)
        {
            if (fromPrice == toPrice)
                return fromPrice.ToShortPrice();
            else
                return string.Format("{0}-{1}", fromPrice.ToShortPrice(), toPrice.ToShortPrice());
        }

        public static string ToShortPrice(this decimal price, int decimalPlaces = 0)
        {
            if (price < 0)
                return "暂无价格";
            //return price.ToString("n" + decimalPlaces);
            return price.ToString("f" + decimalPlaces);
        }

        public static string ToCnDayPrice(this decimal price, string format = "0.00")
        {
            if (price < 0)
                return "暂无报价";

            return string.Format("&yen;{0}/晚起", price.ToString(format));
        }

        public static string ToCnPrice(this decimal price, string format = "0.00")
        {
            if (price < 0)
                return "暂无报价";

            return string.Format("&yen;{0}", price.ToString(format));
        }

        public static string ToStar(this string s, int start = 1)
        {
            var sb = new StringBuilder();

            var firstLetter = s[0];
            var firstIsLetter = 65 < firstLetter && firstLetter < 122;
            if (firstIsLetter)
            {
                var array = s.Split(' ');
                if (array.Length > 1 && array[0].Length <= 10)
                {
                    sb.Append(array[0]);
                    sb.Append(" ");
                    sb.Append(array[1].Substring(0, 1).ToUpper());
                }
                else
                {
                    var head = array[0];
                    if (head.Length > 10)
                        head = s.Substring(0, 10);
                    sb.Append(head);
                    sb.Append("*");
                }
            }
            else
            {
                var head = s.Substring(0, start);
                sb.Append(head);
                sb.Append("**");
            }

            return sb.ToString();
        }

        public static double ToScore(this double score)
        {
            return (score - (int)score) < 0.5 ? (int)score : ((int)score) + 0.5;
        }

        public static Tuple<int, int> ToPriceRange(this string priceParam)
        {
            if (priceParam.Contains("-"))
            {
                var rangeArray = priceParam.Split('-');
                if (rangeArray.Length == 2)
                {
                    var priceRange = new Tuple<int, int>(rangeArray[0].ToInt(), rangeArray[1].ToInt());
                    return priceRange;
                }
            }

            return new Tuple<int, int>(0, 0);
        }

        #region Convert string type to other types
        public static int ToInt(this string s, int defalut = 0)
        {
            int result = defalut;
            if (int.TryParse(s, out result))
                return result;
            else
                return defalut;
        }

        public static bool ToBool(this string s, bool defalut = false)
        {
            bool result = defalut;
            if (bool.TryParse(s, out result))
                return result;
            else
                return defalut;
        }

        public static double ToDouble(this string s, double defalut = 0)
        {
            double result = defalut;
            if (double.TryParse(s, out result))
                return result;
            else
                return defalut;
        }

        public static decimal ToDecimal(this string s, decimal defalut = 0)
        {
            decimal result = defalut;
            if (decimal.TryParse(s, out result))
                return result;
            else
                return defalut;
        }

        public static Guid ToGuid(this string s)
        {
            Guid result = Guid.Empty;
            if (Guid.TryParse(s, out result))
                return result;
            else
                return Guid.Empty;
        }

        public static DateTime ToDateTime(this string s, DateTime defalut = new DateTime())
        {
            DateTime result = defalut;
            if (DateTime.TryParse(s, out result))
                return result;
            else
                return defalut;
        }

        public static T ToEnum<T>(this string s) where T : struct
        {
            T result = default(T);
            Enum.TryParse<T>(s, out result);
            return result;
        }
        #endregion
    }
}
